<template>
    <div class="bbox">
        <div>
            <div class="head">
                <span class="iconfont icon-arrow-left-bold" @click="gotoUp"></span>
                <div class="title">课程详情</div>
            </div>
            <div class="img">
                <img :src="tdata1.thumb" alt="" />
            </div>
            <div class="img-title">{{ tdata1.title }}</div>
            <div class="img-num">
                <div v-if="tdata1.charge > 0"><span>￥</span>{{ tdata1.charge }}</div>
                <div v-else class="active">免费</div>
                <div>{{ tdata1.user_count }}人已订阅</div>
            </div>
            <black :height="10" :background="bgcolor"></black>
            <van-tabs v-model="activeName" title-inactive-color="#333" title-active-color="#3ca7fa">
                <van-tab title="介绍" name="a">
                    <div class="tabs-img">
                        <img :src="this.img" alt="" />
                    </div>
                </van-tab>
                <van-tab title="目录" name="b">
                    <div class="introduce" v-if="token">
                        <introducomone :section1="section1" v-for="(item, index) in title" :key="index" :title="item"></introducomone>
                    </div>
                    <div class="introduce" v-else></div>
                </van-tab>
                <van-tab title="评论" name="c" v-if="token">
                    <commenttab :key="rekey" :comments="comments" :users="users"></commenttab>
                </van-tab>
                <van-tab title="评论" name="c" v-else> </van-tab>
            </van-tabs>
            <div class="footer" v-if="activeName == 'a' || activeName == 'b'">
                <div class="collect" @click="collect" v-if="token">
                    <span :class="{ active: acti ? true : false }" class="iconfont icon-shoucang2"></span>
                    <p>收藏</p>
                </div>
                <div class="collect" v-else>
                    <span :class="{ active: acti ? true : false }" class="iconfont icon-shoucang2"></span>
                    <p>收藏</p>
                </div>
                <div class="box" v-if="token">
                    <div class="look">开始学习</div>
                </div>
                <div class="box" v-else>
                    <div class="vip">VIP会员免费看</div>
                    <div class="take">订阅课程</div>
                </div>
            </div>
            <div class="footer" v-else>
                <div class="input">
                    <input v-model="text" type="text" placeholder="请输入评论内容" />
                </div>
                <div class="button" v-if="token" @click="issue">发布</div>
                <div class="button" v-else>发布</div>
            </div>
        </div>
    </div>
</template>

<script>
import Vue from "vue";
import { Toast } from "vant";
import { Tab, Tabs } from "vant";

Vue.use(Tab).use(Tabs);
import black from "@/components/LittleBlack.vue";
import introducomone from "@/components/IntroduComone.vue";
import commenttab from "@/components/CommentTab.vue";
export default {
    data() {
        return {
            token: "",
            bgcolor: "#F6F6F6",
            activeName: "a",
            acti: true,
            id: 0,
            //总数据
            tab1: {},
            tdata1: {},
            img: "",
            //目录数据
            title: {},
            section: {},
            section1: [],
            // 评论数据
            comments: [],
            users: [],
            //评论内容
            text: "",
            rekey: true,
        };
    },
    methods: {
        gotoUp() {
            this.$router.push({
                path: "/",
                query: {},
            });
        },
        async collect() {
            let like = await this.$request.get(`/api/v2/course/${this.$route.query.id}/like`, {});
            if (like.data == 1) {
                like.data = 0;
                this.acti = true;
            } else {
                like.data = 1;
                this.acti = false;
            }
        },
        issue() {
            if (!this.text) {
                Toast.fail("不能为空");
                return;
            }
            this.$request.post("/api/v2/course/" + this.$route.query.id + "/comment", {
                content: this.text,
            });
            this.rekey = !this.rekey;
            this.text = "";
            setTimeout(() => {
                this.$request
                    .get(`/api/v2/course/${this.$route.query.id}/comments`, {
                        content: this.text,
                    })
                    .then((res) => {
                        this.comments = res.data.comments;
                        this.user = res.data.users;
                    });
            }, 1000);
        },
    },
    components: {
        black,
        introducomone,
        commenttab,
    },
    async created() {
        let token = localStorage.getItem("h5-token");
        if (token) {
            this.token = token;
            await this.$request.get("/api/v2/member/detail", {
                params: {
                    Authorization: `Bearer ${token}`,
                },
            });
        }
        let res = await this.$request.get("/api/v2/course/" + this.$route.query.id, {
            params: {},
        });
        //总数据
        this.tab1 = res.data;
        //指定数据
        this.tdata1 = this.tab1.__ob__.value.course;
        //第二张图片
        this.img = this.tdata1.render_desc.slice(13, -6);
        //目录章节的数据
        this.title = this.tab1.chapters;
        this.section = this.tab1.videos;
        for (var i in this.section) {
            this.section1.push(this.section[i]);
        }
        //评论数据
        let comment = await this.$request.get("/api/v2/course/" + this.$route.query.id + "/comments", {
            params: {},
        });
        this.users = comment.data.users;
        this.comments = comment.data.comments;
    },
};
</script>

<style lang="less" scoped>
/deep/ .van-tabs__wrap {
    height: 55px;
    box-shadow: 0 2px 4px 0 hsl(0deg 0% 80% / 20%);
}

/deep/ .van-tabs__nav {
    z-index: 999;
}

/deep/ .van-tabs__line {
    bottom: 25px;
    width: 20px;
    height: 3px;
    background: #3ca7fa;
    border-radius: 3px;
}

/deep/ .van-tab--active {
    span {
        font-weight: 600;
        color: #3ca7fa;
    }
}

.bbox {
    padding-bottom: 60px;
    .head {
        position: fixed;
        top: 0;
        width: 100%;
        height: 50px;
        background-color: #fff;
        z-index: 100;
        padding-left: 15px;
        padding-right: 15px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-bottom: 1px solid #f1f2f6;
        span {
            font-size: 19px;
            position: absolute;
            top: 50%;
            margin-top: -10px;
            left: 4%;
        }
        .title {
            flex: 1;
            font-size: 17px;
            font-weight: 500;
            text-align: center;
            color: #333;
            line-height: 22px;
            overflow: hidden;
        }
    }
    .img {
        margin-top: 50px;
        width: 100%;
        height: auto;
        img {
            width: 100%;
        }
    }
    .img-title {
        padding: 15px 15px 0 15px;
        font-size: 15px;
        font-weight: 500;
        color: #171923;
        line-height: 15px;
        background-color: #fff;
    }
    .img-num {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 20px 15px 15px 15px;
        font-size: 20px;
        line-height: 30px;
        background-color: #fff;
        div:nth-child(1) {
            color: #ff4d4f;
            font-weight: 500;
            span {
                font-size: 14px;
            }
            &.active {
                color: #04c877 !important;
            }
        }
        div:nth-child(2) {
            color: #999;
            font-weight: 400;
            font-size: 14px;
        }
    }
    .tabs-img {
        width: 100%;
        color: #333;
        padding: 20px 15px 15px 15px;
        img {
            width: 100%;
        }
    }
    .introduce {
        width: 100%;
        color: #333;
        line-height: 21px;
        padding: 10px 15px 15px 15px;
        .introduce-item {
            width: 100%;
            .introduce-title {
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: space-between;
                font-size: 15px;
                color: #333;
                font-weight: 700;
                margin-bottom: 20px;
                margin-top: 10px;
            }
            .introduce-box {
                width: 100%;
                .box-item {
                    width: 100%;
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    margin-bottom: 10px;
                    line-height: 36px;
                    padding: 0 10px 0 10px;
                    background: #f6f6f6;
                    border-radius: 4px;
                    .item-title {
                        flex: 1;
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        font-size: 14px;
                        color: #333;
                        span:nth-child(1) {
                            padding: 2px 4px;
                            background-color: #04c877;
                            border-radius: 2px;
                            color: #fff;
                            font-size: 12px;
                            margin-right: 10px;
                        }
                        span:nth-child(2) {
                            flex: 1;
                            white-space: nowrap;
                            overflow: hidden;
                            text-overflow: ellipsis;
                            font-size: 14px;
                            color: #333;
                        }
                    }
                    .item-time {
                        width: auto;
                        height: auto;
                        font-size: 12px;
                        margin-left: 10px;
                        color: #999;
                    }
                }
            }
        }
    }

    .footer {
        width: 100%;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 11;
        height: 53px;
        background-color: #fff;
        border-top: 1px solid rgba(0, 0, 0, 0.1);
        display: flex;
        padding-left: 15px;
        padding-right: 15px;
        .input {
            flex: 1;
            padding-top: 10px;
            input {
                width: 100%;
                height: 22px;
                font-size: 14px;
                font-weight: 400;
                color: #333;
                line-height: 22px;
                margin-top: 8px;
                border: none;
            }
        }
        .button {
            width: auto;
            height: 33px;
            font-size: 14px;
            font-weight: 400;
            color: #666;
            line-height: 33px;
            padding-left: 15px;
            padding-top: 10px;
        }
        .collect {
            width: 25px;
            margin-right: 15px;
            span {
                display: block;
                margin-top: 5px;
                font-size: 25px;
                text-align: center;
                margin-bottom: 1px;
                &.active {
                    color: yellow;
                }
            }
            p {
                width: 100%;
                height: 25px;
                line-height: 25px;
                font-size: 10px;
                font-weight: 400;
                color: #666;
                text-align: center;
            }
        }
        .box {
            flex: 1;
            display: flex;
            .look {
                border-radius: 3px;
                background-color: #3ca7fa;
                color: #fff;
                flex: 1;
                display: block;
                text-align: center;
                margin: 4px 0;
                line-height: 40px;
                font-size: 15px;
                font-weight: 500;
                color: #fff;
                align-items: center;
                justify-content: center;
            }
            .vip {
                border-radius: 3px 0 0 3px;
                background-color: #e2a500;
                color: #fff;
                flex: 1;
                text-align: center;
                margin: 4px 0;
                line-height: 40px;
                font-size: 15px;
                font-weight: 500;
                display: flex;
                align-items: center;
                justify-content: center;
            }
            .take {
                border-radius: 0 4px 4px 0;
                background-color: #ff5068;
                color: #fff;
                flex: 1;
                text-align: center;
                margin: 4px 0;
                line-height: 40px;
                font-size: 15px;
                font-weight: 500;
                display: flex;
                align-items: center;
                justify-content: center;
            }
        }
    }
}
</style>
